package com.example.shouybl.ui;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RatingBar;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.example.shouybl.R;
import com.example.shouybl.adapter.VpiPageAdapter;
import com.example.shouybl.bean.MoneyData;
import com.example.shouybl.bean.MoneyGame;
import com.example.shouybl.bean.MoneyGameBase;
import com.example.shouybl.constant.Path;
import com.example.shouybl.fragment.CommentFragment;
import com.example.shouybl.fragment.GameGiftFragment;
import com.example.shouybl.fragment.GameNewsFragment;
import com.example.shouybl.utils.ImageLoaderUtil;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.RequestParams;
import com.loopj.android.http.TextHttpResponseHandler;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.viewpagerindicator.TabPageIndicator;

import java.util.ArrayList;

import cz.msebera.android.httpclient.Header;

public class MoneyDetailActivity extends AppCompatActivity {


    private MoneyData data;
    private com.viewpagerindicator.TabPageIndicator tbi;
    private android.support.v4.view.ViewPager vp;
    private android.widget.Button btnmoneydetail;
    private android.widget.ImageView ivmgameicon;
    private android.widget.TextView tvmgamename;
    private android.widget.RatingBar rbmgame;
    private android.widget.TextView tvmgamemsg;
    private ArrayList<Fragment> fragments=new ArrayList<>();
    private String[] title={"游戏信息","游戏礼包","评论"};
    private VpiPageAdapter adapter;
    private GameNewsFragment newsFragment=new GameNewsFragment();
    private GameGiftFragment giftFragment=new GameGiftFragment();
    private CommentFragment commentFragment=new CommentFragment();

    private MoneyGame game=new MoneyGame();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_money_detail);

        data= (MoneyData) getIntent().getExtras().getSerializable("money");
        initView();
        initData();
        //传值
        passData();
        //添加fragment
        fragments.add(newsFragment);
        fragments.add(giftFragment);
        fragments.add(commentFragment);
        adapter = new VpiPageAdapter(getSupportFragmentManager(),fragments,title);
        vp.setAdapter(adapter);
        tbi.setViewPager(vp);
    }

    private void initData() {
        AsyncHttpClient client = new AsyncHttpClient();
        //游戏详情数据
        client.post(Path.PATH_MONEY_DETAIL, getMoneyParams(), new TextHttpResponseHandler() {
            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {

            }

            @Override
            public void onSuccess(int statusCode, Header[] headers, String responseString) {
                TypeReference<MoneyGameBase> type = new TypeReference<MoneyGameBase>() {
                };
                MoneyGameBase gameBase = JSONObject.parseObject(responseString, MoneyGameBase.class);
                game = gameBase.getInfo();
                tvmgamename.setText(game.getName());
                tvmgamemsg.setText("剩余:"+game.getVersion()+" | "+game.getSize()+" "+game.getCount_dl()+"下载");
                if (game.getIcon()!=null){
                    ImageLoader.getInstance().displayImage(game.getIcon(),ivmgameicon, ImageLoaderUtil.getDefaultOptions());
                }
            }
        });
    }
    private RequestParams getMoneyParams() {
        RequestParams params = new RequestParams();
        params.add("id", data.getId());
        params.add("compare", "02746f27bbc4108a98605b192ea140ea");
        return params;
    }


    /**
     * 传值给所含fragment
     */
    private void passData() {
        Bundle args=new Bundle();
        args.putString("id",data.getId());
        //args.putString("content",game.getGame_desc());
        newsFragment.setArguments(args);
        giftFragment.setArguments(args);
        commentFragment.setArguments(args);
    }

    private void initView() {
        this.tvmgamemsg = (TextView) findViewById(R.id.tv_mgame_msg);
        this.rbmgame = (RatingBar) findViewById(R.id.rb_mgame);
        this.tvmgamename = (TextView) findViewById(R.id.tv_mgame_name);
        this.ivmgameicon = (ImageView) findViewById(R.id.iv_mgame_icon);
        this.btnmoneydetail = (Button) findViewById(R.id.btn_money_detail);
        this.vp = (ViewPager) findViewById(R.id.vp_money_detail);
        this.tbi = (TabPageIndicator) findViewById(R.id.tbi);
    }



}
